package ks.client.gui.admin.user.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.w3c.dom.Document;
import ks.client.UserContext;
import ks.client.gui.ErrorMessageGUI;
import ks.client.ipc.Client;
import ks.client.lobby.Lobby;
import ks.framework.common.Message;


/**
 * 
 * When the "Block User" button is clicked, a command is sent to the server
 * to block the user defined by the input user name.
 * 
 * @author Jennifer Hunt
 */
public class BlockUserClickController implements ActionListener{
	
	Lobby lobby;
	JTextField username;
	
	/**
	 * Constructor for BlcokUserClickController
	 * @param lobby
	 */
	public BlockUserClickController(Lobby lobby, JTextField username) {
		this.lobby = lobby;
		this.username = username;	
	}
	
	/**
	 * 
	 * Constructs and sends the command to block the user to the server.
	 * If no user name was entered an error message appears. 
	 * 
	 */
	public void actionPerformed(ActionEvent e){
		if(username.getText().isEmpty()){
			JFrame pma = new ErrorMessageGUI("Please type a username.");
			pma.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			pma.setTitle("Error");
			pma.setVisible(true);
		}
		else{
			String s = "<admin-command version= \"1.0\" id=\"589a39591271844e3fbe32bbb9281ad9\">" +
			"<user-block player=\"" + username.getText() + "\"/>" + 
			"</admin-command>";			
	
			Document d = Message.construct(s);
			Message m = new Message(d);

			//send to server
			UserContext uc = lobby.getUserContext();
			Client c = uc.getClient();
			c.sendToServer(m);
		}
	}
}
